CODE 76. Insert Interval

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2013/10/11/2013-10-11-CODE 76 Insert Interval/

访问原文「CODE 76. Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
You may assume that the intervals were initially sorted according to their start times.
Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in
as [1,5],[6,9].
Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in
as [1,2],[3,10],[12,16].
This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
public ArrayList<Interval> insert(ArrayList<Interval> intervals,
Interval newInterval) {
// Note: The Solution object is instantiated only once and is reused by
// each test case.
if (null == intervals || intervals.isEmpty()) {
ArrayList<Interval> newIntervals = new ArrayList<Interval>();
if (null != newInterval) {
newIntervals.add(newInterval);
}
return newIntervals;
}
int i;
for (i = 0; i < intervals.size(); i++) {
if (newInterval.start >= intervals.get(i).start
&& newInterval.start <= intervals.get(i).end
&& newInterval.end >= intervals.get(i).end) {
intervals.get(i).end = newInterval.end;
break;
} else if (newInterval.end >= intervals.get(i).start
&& newInterval.end <= intervals.get(i).end
&& newInterval.start <= intervals.get(i).start) {
intervals.get(i).start = newInterval.start;
break;
} else if (newInterval.start >= intervals.get(i).start
&& newInterval.end <= intervals.get(i).end) {
return intervals;
} else if (newInterval.start <= intervals.get(i).start
&& newInterval.end >= intervals.get(i).end) {
intervals.get(i).start = newInterval.start;
intervals.get(i).end = newInterval.end;
break;
} else if (newInterval.end < intervals.get(i).start) {
intervals.add(i, newInterval);
break;
}
}
if (i >= intervals.size()) {
intervals.add(newInterval);
return intervals;
}
ArrayList<Interval> newIntervals = new ArrayList<Interval>();
Interval first = intervals.get(0);
for (i = 1; i < intervals.size(); i++) {
if (null == first) {
first = intervals.get(i);
} else if (intervals.get(i).start >= first.start
&& intervals.get(i).start <= first.end
&& intervals.get(i).end >= first.end) {
first.end = intervals.get(i).end;
} else if (intervals.get(i).end >= first.start
&& intervals.get(i).end <= first.end
&& intervals.get(i).start <= first.start) {
first.start = intervals.get(i).start;
} else if (intervals.get(i).start >= first.start
&& intervals.get(i).end <= first.end) {
continue;
} else if (intervals.get(i).start <= first.start
&& intervals.get(i).end >= first.end) {
first.start = intervals.get(i).start;
first.end = intervals.get(i).end;
} else {
newIntervals.add(first);
first = null;
first = intervals.get(i);
}
}
if (null != first) {
newIntervals.add(first);
}
return newIntervals;
}
Jerky Lu wechat
欢迎加入微信公众号